package com.hzit.controller;

import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;

	//1. 查询所有学生列表
	@GetMapping("/list")
	public R findAll(){
		List<StudentVo> studentVoList = studentService.findAll();
		return R.ok(studentVoList);
	}

	//2. 分页查询
	@GetMapping("/findByPage")
	public R findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
		PageResult<StudentVo> pr = studentService.findByPage(page,pageSize);
		return R.ok(pr);
	}

	//3. 条件查询带分页
	//@ResponseBody与@RequestBody区别：
	//① 前者是将Java对象转成JSON字符串并输出
	//② 后者是将前端传入的JSON对象转换java对象
	@PostMapping("/search")
	public R search(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize,@RequestBody StudentVo vo){
		PageResult<StudentVo> pr = studentService.search(page,pageSize,vo);
		return R.ok(pr);
	}

	//4. 添加学生
	@PostMapping("/add")
	public R add(@RequestBody StudentVo vo){
		System.out.println("vo = " + vo);
		studentService.add(vo);
		return R.ok("添加学生成功！");
	}

	//5. 修改学生
	@PostMapping("/update")
	public R update(@RequestBody StudentVo vo){
		studentService.update(vo);
		return R.ok("修改学生成功");
	}

	//6. 删除学生
	@GetMapping("/delete")
	public R delete(Integer[] sids){
		studentService.delete(sids);
		return R.ok("删除学生成功");
	}

}
